//
//  LoginViewController.m
//  MiAiApp
//
//  Created by voidcat on 2017/5/18.
//  Copyright © 2017年 voidcat. All rights reserved.
//

#import "LoginViewController.h"
#import <UMSocialCore/UMSocialCore.h>
#import "MobileLogin.h"
#import "FillingDataViewController.h"
#import "RootWebViewController.h"
@interface LoginViewController ()
@property(nonatomic, strong) UIButton *mobileLogin;//手机登录按钮
@property(nonatomic, strong) UIButton *wechatLogin;//微信登录图标
@property(nonatomic, strong) UILabel *otherLable;//其他登录lable
@property(nonatomic, strong) MobileLogin *mobieLoginiView;
@property(nonatomic, strong) QMUIFillButton *doWechatLoign;//微信一键登录
@property(nonatomic, strong) QMUILinkButton *privacyPolicyLab;//隐私政策


@end
//loginVideo
@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //隐藏导航栏
    self.isHidenNaviBar = YES;
    self.title = ASLocalizedString(@"登录");
    


    //登录背景
//    UIImageView *bg = [[UIImageView alloc] init];
//    bg.image = IMAGE_NAMED(@"loading");
//    bg.frame = self.view.bounds;
//    [self.view addSubview:bg];

    int showBtnWidth = 150;
    YYLabel *snowBtn = [[YYLabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2 - showBtnWidth/2, 240, 150, 60)];
    snowBtn.text = ASLocalizedString(@"微信登录");
    snowBtn.font = SYSTEMFONT(20);
    snowBtn.textColor = KWhiteColor;
    snowBtn.backgroundColor = CNavBgColor;
    snowBtn.textAlignment = NSTextAlignmentCenter;
    snowBtn.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    snowBtn.centerX = KScreenWidth/4;
    
    kWeakSelf(self);
    snowBtn.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weakself WXLogin];
    };
    
    
    YYLabel *snowBtn2 = [[YYLabel alloc] initWithFrame:CGRectMake(0, 300, 150, 60)];
    snowBtn2.text = ASLocalizedString(@"QQ登录");
    snowBtn2.font = SYSTEMFONT(20);
    snowBtn2.textColor = KWhiteColor;
    snowBtn2.backgroundColor = KRedColor;
    snowBtn2.textAlignment = NSTextAlignmentCenter;
    snowBtn2.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    snowBtn2.centerX = KScreenWidth/2;
    
    snowBtn2.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weakself QQLogin];
    };


    _otherLable = [[UILabel alloc] init];
    _otherLable.text = ASLocalizedString(@"其他登录方式");
    _otherLable.font = SYSTEMFONT(13);
    _otherLable.textColor = [UIColor colorWithHexString:@"C3B1B0"];
    
    _mobileLogin = [[UIButton alloc] init];
    [_mobileLogin setImage:IMAGE_NAMED(@"login_phone") forState:UIControlStateNormal];
    
    _wechatLogin = [[UIButton alloc] init];
    _wechatLogin.alpha = 0.8;
    [_wechatLogin setImage:IMAGE_NAMED(@"login_weChat") forState:UIControlStateNormal];
    
    if(KGlobalVariable.appmodel.open_video_chat == 0)
    {
        _wechatLogin.hidden = YES;
    }
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(removieLoginView)
                                                 name:KinitConfigLoginSuccess
                                               object:nil];

    [_mobileLogin addTapBlock:^(UIButton *btn) {
        [weakself showLoginView:@"mobile"];
    }];
    
    [_wechatLogin addTapBlock:^(UIButton *btn) {
        [weakself showLoginView:@"wechat"];
    }];
    
    _doWechatLoign = [[QMUIFillButton alloc] initWithFillColor:MainColor titleTextColor:KWhiteColor];
    [_doWechatLoign setTitle:ASLocalizedString(@"微信登录")forState:UIControlStateNormal];
    [_doWechatLoign addTarget:self action:@selector(WXLogin) forControlEvents:UIControlEventTouchUpInside];
    _doWechatLoign.alpha = 0.8;
    
    [self.view addSubview:_otherLable];
    [self.view addSubview:_mobileLogin];
    [self.view addSubview:_wechatLogin];
    [self.view addSubview:_doWechatLoign];

    [self showLoginView:@"mobile"];
    
    self.privacyPolicyLab = [[QMUILinkButton alloc] init];
    [self.privacyPolicyLab setTitle:ASLocalizedString(@"登录既代表您同意隐私政策,点击查看更多")forState:UIControlStateNormal];
    self.privacyPolicyLab.titleLabel.font = [UIFont systemFontOfSize:11];
    [self.privacyPolicyLab setTitleColor:MainColor forState:UIControlStateNormal];
    [self.privacyPolicyLab addTarget:self action:@selector(handlePPBEvent) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.privacyPolicyLab];
    [self setLayoutView];
    [self removieLoginView];
    
    _otherLable.hidden = YES;
    _wechatLogin.hidden = YES;
    _doWechatLoign.hidden = YES;
    _mobileLogin.hidden = YES;
}
//隐私政策
- (void) handlePPBEvent{
    NSString *url = [URL_main stringByAppendingString:@"/admin/public/portal/article/index/id/7.html"];
    RootWebViewController *webView = [[RootWebViewController alloc] init];
    webView.url = url;
    [self.navigationController pushViewController:webView animated:YES];
}

- (void)removieLoginView {
    if(KGlobalVariable.appmodel.is_ios_base == 1)
    {
        _wechatLogin.hidden = YES;
        _otherLable.hidden = YES;
    }
    else
    {
        _privacyPolicyLab.hidden =  NO;
    }
}

-(void)WXLogin{
    [userManager login:kUserLoginTypeWeChat completion:^(BOOL success, NSString *des) {
        if (success) {
            if([curUser.is_reg_perfect isEqualToString:@"0"])
            {
                FillingDataViewController *loginFillData = [FillingDataViewController new];
                [self presentViewController:loginFillData animated:NO completion:^{
                    
                }];
            }
            else
            {
                KPostNotification(KNotificationLoginStateChange, @YES);
            }
        
//            KPostNotification(KNotificationLoginStateChange, @YES);
            DLog(ASLocalizedString(@"登录成功"));
        }else{
            DLog(ASLocalizedString(@"登录失败：%@"), des);
        }
    }];
}

-(void)QQLogin{
    [userManager login:kUserLoginTypeQQ completion:^(BOOL success, NSString *des) {
        if (success) {
            DLog(ASLocalizedString(@"登录成功"));
        }else{
            DLog(ASLocalizedString(@"登录失败：%@"), des);
        }
    }];
}

-(void)skipAction{
    KPostNotification(KNotificationLoginStateChange, @YES);
}

-(void)setLayoutView
{
    //其他登录lable
    [_otherLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_mobileLogin.mas_top).offset(-25);
        make.centerX.equalTo(self.view);
    }];
    //手机登录图标
    [_mobileLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-80);
        make.width.and.height.equalTo(@60);
    }];
    
    //微信登录图标
    [_wechatLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-80);
        make.width.and.height.equalTo(@60);
    }];
    
    
    //微信一键登录
    [_doWechatLoign mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).offset(-25);
        make.height.equalTo(@40);
        make.left.equalTo(self.view).offset(35);
        make.right.equalTo(self.view).offset(-35);
    }];
    
    //隐私政策
    [_privacyPolicyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_mobileLogin.mas_bottom).offset(10);
        make.height.equalTo(@10);
        make.centerX.equalTo(_mobileLogin);
    }];
}

-(void)showLoginView:(NSString *)type
{
    _wechatLogin.hidden = YES;
    _mobileLogin.hidden = YES;
    if([type isEqualToString:@"mobile"])
    {
        self.mobieLoginiView.hidden = NO;
        self.doWechatLoign.hidden = YES;
        _wechatLogin.hidden = NO;
    }
    else
    {
        //显示微信登录的界面
        self.mobieLoginiView.hidden = YES;
        self.doWechatLoign.hidden = NO;
        _mobileLogin.hidden = NO;
    }
}

-(MobileLogin *)mobieLoginiView
{
    if(_mobieLoginiView == nil)
    {
        _mobieLoginiView = [MobileLogin getView];
        [self.view addSubview:_mobieLoginiView];
        [_mobieLoginiView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(100);
            make.width.equalTo(self.view);
            make.height.equalTo(@(KScreenHeight/3));
            make.centerX.equalTo(self.view);
        }];
        
        //暂时保留
        kWeakSelf(self);
        _mobieLoginiView.loginHasFillData = ^{
            FillingDataViewController *loginFillData = [FillingDataViewController new];
            [weakself presentViewController:loginFillData animated:NO completion:^{

            }];
        };
//
//        _mobieLoginiView.loginSuccess = ^{
//            //填充资料
//            FillingDataViewController *loginFillData = [FillingDataViewController new];
//            [weakself presentViewController:loginFillData animated:NO completion:^{
//
//            }];
//
////            KPostNotification(KNotificationLoginStateChange, @YES);
//        };
    }
    else
    {
    }
    
    return _mobieLoginiView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
